﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.SqlClient;
using Microsoft.Data.Schema.UnitTesting;
using System.Threading;
using QueueUnlimited.ScenarioTests.Helper;

namespace QueueUnlimited.ScenarioTests
{
    /// <summary>
    /// </summary>
    [TestClass]
    public class Subscription_Scenarios : BaseScenarioTestFixture
    {
        [TestMethod]
        public void Test_Message_is_enqueued_succesfully()
        {
            using (var dataStore = new API.DataStore(ConnectionStringForTests))
            {
                dataStore.Enqueue("SomeEvent", "Ola Pola");
            }
        }

        [TestMethod]
        public void Test_Message_is_enqueued_succesfully_and_root_service_contains_one_message()
        {
            using (var dataStore = new API.DataStore(ConnectionStringForTests))
            {
                dataStore.Enqueue("SomeEvent", "Ola Pola");
                Assert.AreEqual(1, dataStore.CountOfUnprocessedMessages());
            }

        }

        [TestMethod]
        public void Test_Message_is_enqueued_and_subscribers_are_added_and_receive_the_message()
        {
            using (var dataStore = new API.DataStore(ConnectionStringForTests))
            {
                dataStore.Enqueue("SomeEvent", "Ola Pola");
                Assert.AreEqual(1, dataStore.CountOfUnprocessedMessages());

                var firstSubscriber = new API.Subscriber("MyFirstSubscriber", "SomeEvent");
                dataStore.Configuration.Apply(firstSubscriber); // add subscriber
                // give the system some slack
                Thread.Sleep(5 * 1000);

                Assert.AreEqual(1, dataStore.Count(firstSubscriber));

                var secondSubscriber = new API.Subscriber("MySecondSubscriber", "SomeEvent");
                dataStore.Configuration.Apply(secondSubscriber); // add subscriber
                // give the system some slack
                Thread.Sleep(5 * 1000);

                Assert.AreEqual(1, dataStore.Count(secondSubscriber));

                Assert.AreEqual(0, dataStore.CountOfUnprocessedMessages());
            }
        }

        [TestMethod]
        public void Bug_Duplicate_Message()
        {
            using (var dataStore = new API.DataStore(ConnectionStringForTests))
            {
                var firstSubscriber = new API.Subscriber("MyFirstSubscriber1", "SomeEvent");
                dataStore.Configuration.Apply(firstSubscriber); // add subscriber

                dataStore.Enqueue("SomeEvent", "Ola Pola1");
                dataStore.Enqueue("SomeEvent", "Ola Pola2");
                dataStore.Enqueue("SomeEvent", "Ola Pola3");
                dataStore.Enqueue("SomeEvent", "Ola Pola4");
                dataStore.Enqueue("SomeEvent", "Ola Pola5");

                // give the system some slack
                Thread.Sleep(2 * 1000);

                Assert.AreEqual(0, dataStore.CountOfUnprocessedMessages());
                Assert.AreEqual(5, dataStore.Count(firstSubscriber));
            }
        }
    }
}
